<template>
  <div class="user-edit-container">
    <!-- Header 区域 -->
    <van-nav-bar
      title="编辑资料"
      left-arrow
      @click-left="$router.back()"
      fixed
    />

    <!-- 用户资料 -->
    <van-cell-group class="action-card">
      <van-cell title="头像" is-link center>
        <template #default>
          <van-image
            round
            class="avatar"
            :src="profileObj.photo"
            @click="imageClickFn"
          />
          <!-- file 选择框 -->
          <input
            type="file"
            ref="iptFile"
            v-show="false"
            accept="image/*"
            @change="onFileChange"
          />
        </template>
      </van-cell>
      <van-cell
        title="名称"
        is-link
        :value="profileObj.name"
        @click="updateNameFn"
      />
      <van-cell
        title="生日"
        is-link
        :value="profileObj.birthday"
        @click="updateBirthdayFn"
      />
    </van-cell-group>

    <!-- 修改名字 -->
    <van-dialog
      v-model="show"
      title="标题"
      show-cancel-button
      :beforeClose="beforeCloseFn"
    >
      <van-field v-model="newName" value="123" input-align="center" v-fofo />
    </van-dialog>

    <!-- 修改年龄 -->
    <van-popup
      round
      v-model="theDate"
      position="bottom"
      :style="{ height: '50%' }"
    >
      <van-datetime-picker
        v-model="currentDate"
        type="date"
        title="选择年月日"
        :min-date="minDate"
        :max-date="maxDate"
        @confirm="dateConfirm"
        @cancel="dateCancel"
      />
    </van-popup>
  </div>
</template>

<script>
import { reqUserProfile, reqUpdateUserPhoto, reqUpdateUserInfo } from "@/api";
import { Notify } from "vant";
import { dateFormat } from "@/utils/day";
export default {
  name: "UserEdit",
  data() {
    return {
      profileObj: {},
      show: false,
      newName: "",
      theDate: false,
      minDate: new Date(1980, 0, 1),
      maxDate: new Date(),
      currentDate: new Date(),
    };
  },
  created() {
    this.getData();
  },
  methods: {
    async getData() {
      const res = await reqUserProfile();
      if (res.status == 200) {
        this.profileObj = res.data.data;
      }
    },
    imageClickFn() {
      this.$refs.iptFile.click();
    },
    async onFileChange(e) {
      if (e.target.files.length == 0) {
        return;
      }
      const fd = new FormData();
      fd.append("photo", e.target.files[0]);
      const res = await reqUpdateUserPhoto(fd);
      if (res.status == 200) {
        this.profileObj.photo = res.data.data.photo;
      }
    },
    updateNameFn() {
      this.show = true;
      this.newName = this.profileObj.name;
    },
    async beforeCloseFn(action, done) {
      if (action === "confirm") {
        const reg = /^[A-Za-z0-9\u4e00-\u9fa5]{1,14}$/;
        if (reg.test(this.newName) == true) {
          await reqUpdateUserInfo({
            name: this.newName,
          });
          this.profileObj.name = this.newName;
          done();
        } else {
          Notify({ type: "warning", message: "请输入1-14位的中英数字的组合" });
          done(false);
        }
      } else {
        done();
      }
    },
    updateBirthdayFn() {
      this.theDate = true;
      this.currentDate = new Date(this.profileObj.birthday);
    },
    async dateConfirm() {
      await reqUpdateUserInfo({
        birthday: dateFormat(this.currentDate),
      });
      this.profileObj.birthday = dateFormat(this.currentDate);
      this.theDate = false;
    },
    dateCancel() {
      this.theDate = false;
    },
  },
};
</script>

<style lang="less" scoped>
.user-edit-container {
  padding-top: 46px;
  .avatar {
    width: 50px;
    height: 50px;
  }
}
</style>
